#!/usr/bin/env python

"""
DESCRIPTION & USAGE:
This script needs Tkinter. It will create a GUI with available options
for easy dropshadow creation. You'll get a group of object that will have incremental shade or opacity value that simulates roughly a shadow blur. Best print result if at least quality = attenuation*3

Steps to create:
    1) Fill requested values in the dropshadow dialog

AUTHOR:
    Cedric Gemy <cedric.gemy [at] gmail [dot] com>

LICENSE:
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

"""
#TODO
#detect and apply on groups
#detect and apply on Textframe by converting to outline


from scribus import *
try:
	from Tkinter import *
	from tkFont import Font
except ImportError:
	print "This script requires Python's Tkinter properly installed."
	messageBox('Script failed','This script requires Python\'s Tkinter properly installed.', ICON_CRITICAL)
	sys.exit(1)

class App:

	def __init__(self, master):
		self.topobject=master
		
		self.dx = IntVar()
		self.dy = IntVar()
		self.opacityV = IntVar()
		self.qualityV = IntVar()
		self.attenuationV = DoubleVar()
		self.StypeV=BooleanVar()
		self.colorV= StringVar()
		
		self.dx.set(2)
		self.dy.set(2)
		self.opacityV.set(75)
		self.qualityV.set(10)
		self.attenuationV.set(2.0)
		self.StypeV.set(FALSE)
		self.colorV= ("Black")

		self.uidesign()

	def uidesign(self):
		frame = Frame(self.topobject,width=200,height=220)
		self.topobject.title("DropShadow")
		self.topobject.geometry('200x220-400+400')
		self.topobject.resizable(FALSE,FALSE)
		
		self.labeldistancex=Label(frame, text="DistanceX")
		self.distancex = Spinbox(frame, textvariable=self.dx,width=2, from_=-99, to=99)
		
		self.labeldistancey=Label(frame, text="DistanceY")
		self.distancey = Spinbox(frame, textvariable=self.dy,width=2, from_=-99, to=99)
		
		self.labeln=Label(frame, text="Quality")
		self.n=Spinbox(frame,textvariable=self.qualityV, width=2, from_=1, to=256)
		
		self.labelattenuation=Label(frame, text="Attenuation")
		self.attenuation= Spinbox(frame, textvariable=self.attenuationV,width=2, from_=0.0, to=100.0)
		
		self.labelopacity=Label(frame,text="Opacity/Shade")
		#self.opacity=Spinbox(frame,textvariable=self.opacityV, width=3, from_=1, to=100)
		self.opacity = Scale(frame, variable=self.opacityV,orient=HORIZONTAL, length=70, from_=1.0, to=100.0)
		
		self.Stype = Checkbutton(frame, text="Userealtransparency", variable=self.StypeV)
		
		self.labelcolor=Label(frame,text="Color")
		self.color = Listbox(frame, height=2,width=10)
		couleurs=getColorNames()
		for couleur in couleurs:
			self.color.insert(END,couleur)
		self.color.selection_set(0)
		
		self.button = Button(frame, text="Cancel", command=self.quit)
		self.apply = Button(frame, text="Apply", command=self.applyEffect)
		#self.clear = Button(frame, text="Clear", command=self.clear)
		
		
		self.labeldistancex.pack()
		self.labeldistancex.place(x=10,y=0)
		self.distancex.pack()
		self.distancex.place(x=110,y=0)
		self.labeldistancey.pack()
		self.labeldistancey.place(x=10,y=20)
		self.distancey.pack()
		self.distancey.place(x=110,y=20)
		self.labeln.place(x=10,y=40)
		self.n.place(x=110,y=40)
		self.labelattenuation.place(x=10,y=60)
		self.attenuation.place(x=110,y=60)
		self.labelopacity.place(x=10,y=100)
		self.opacity.place(x=110,y=80)
		self.Stype.place(x=10,y=120)
		self.labelcolor.place(x=10,y=150)
		self.color.place(x=110,y=150)
		self.button.place(x=10,y=190)
		self.apply.place(x=110,y=190)
		#self.clear.place(x=60,y=190)
		frame.pack()

	def clear():
		#clearshadow
		if selectionCount()==1:
			deleteObject(getSelectedObject())
		else:
			messageBox('Select an object',ICON_CRITICAL)
		self.topobject.destroy()

	def applyEffect(self):
		if selectionCount()==1:
			#getvalues
			n=self.qualityV.get()
			dx=self.dx.get() 
			dy=self.dy.get()
			color=self.color.get(ACTIVE)
			opacity=float(self.opacityV.get())/100
			attenuation=self.attenuationV.get() #
			if getCornerRadius()<1:
				radius=5 #in pt
			else:
				radius=getCornerRadius()
			i=0
			decal=attenuation/n
			li=[]
			
			docUnit=getUnit()
			setUnit(1)#points (0). (1) is millimeters (2) is inches and (3) is picas.
			
			for i in range(0,n):
				duplicateObject()
				if i>0:
					moveObject(decal+(decal/i),decal+(decal/i))
					(width,height)=getSize()
					sizeObject(width-(((decal+(decal/i))*2)),height-(((decal+(decal/i))*2)))
					#setCornerRadius((attenuation-(decal*i))*3.5)
				else:
					moveObject(dx+attenuation,dy+attenuation)
					#setCornerRadius((attenuation+abs(dx))*3.5)
				setFillColor(color)
				if self.StypeV.get()==TRUE:
					setFillTransparency(opacity/n)
				else:
					setFillShade(opacity*(i*100/n))
				setLineColor('None')
				li.append(getSelectedObject())
				i=i+1
			for i in li:
				selectObject(i)
			groupObjects()
			moveSelectionToBack()
			setUnit(docUnit)
			#redrawAll()
			self.quit()
		else:
			messageBox('Select an object',ICON_CRITICAL)

	def quit(self):
		self.topobject.destroy()
		self.quit()

root = Tk()

app = App(root)

root.mainloop()

##############################
#HARD TO FIND SO KEEP IT HERE
# Pega tipo do objeto
# Entrada: objeto 
# Retorno:
# 0: nada selecionado
# 2: quadro de Imagem ou renderiz
# 4: quadro de Texto
# 5: linha
# 6: grupo ou rect
# 7: polygon ou polyline
# 8: texto em linha
##############################

